﻿using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(AnimationClipEvent))]
public class ModelEventEditor : Editor
{
    int tabIndex = 0;
    AnimationClipEvent data;
    ModelEventTab modelEventTab;
    FunctionTab functionTab;
    List<TabEditor> tabs = new List<TabEditor>();
    private void OnEnable()
    {
        data = target as AnimationClipEvent;
        modelEventTab = new ModelEventTab(this, data);
        functionTab = new FunctionTab(data);
        if (data.animatorController != null)
        {
            UpdateClips(false);
        }

        tabs.Add(new TabEditor { name = "事件" }.AddAction(modelEventTab.Draw));
        tabs.Add(new TabEditor { name = "方法" }.AddAction(functionTab.Draw));
    }

    public override void OnInspectorGUI()
    {
        DrawModel();
        DrawTab();
        tabs[tabIndex].Update();

        if (GUI.changed)
        {
            EditorUtility.SetDirty(target);
        }
    }

    public override void OnPreviewSettings()
    {
        modelEventTab.OnPreviewSettings();
    }

    public override void OnInteractivePreviewGUI(Rect r, GUIStyle background)
    {
        modelEventTab.OnInteractivePreviewGUI(r,background);
    }

    public override bool HasPreviewGUI()
    {
        return modelEventTab.HasPreviewGUI();
    }

    void DrawTab()
    {
        EditorGUILayout.BeginHorizontal();
        {
            for (int i = 0; i < tabs.Count; i++)
            {
                if (GUILayout.Button(tabs[i].name))
                {
                    tabIndex = i;
                }
            }
        }
        EditorGUILayout.EndHorizontal();
    }

    void DrawModel()
    {
        EditorGUI.BeginChangeCheck();
        data.animationModel = EditorGUILayout.ObjectField(new GUIContent("动画模型"), data.animationModel, typeof(GameObject), false) as GameObject;
        if (EditorGUI.EndChangeCheck())
        {
            var modelPath = AssetDatabase.GetAssetPath(data.animationModel).ToLower();
            if (Path.GetExtension(modelPath) != ".fbx")
            {
                data.animationModel = null;
            }
            else
            {
                UpdateClips(true);
                modelEventTab.UpdateSelect(0);
            }
        }

        EditorGUI.BeginChangeCheck();
        data.animatorController = EditorGUILayout.ObjectField(new GUIContent("动画状态机"), data.animatorController, typeof(RuntimeAnimatorController), false) as RuntimeAnimatorController;
        if (EditorGUI.EndChangeCheck())
        {
            if (data.animatorController != null && data.animatorController.GetType() == typeof(AnimatorOverrideController))
            {
                data.animatorController = null;
            }
            else
            {
                UpdateClips(false);
                modelEventTab.UpdateSelect(0);
            }
        }
    }

    void UpdateClips(bool isUpdateModel)
    {
        Dictionary<string, AnimationClip> animationDic = new Dictionary<string, AnimationClip>();
        if (isUpdateModel)
        {
            if (data.animationModel != null)
            {
                var array = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(data.animationModel));
                foreach (var item in array)
                {
                    if (item is AnimationClip)
                    {
                        animationDic.Add(item.name, item as AnimationClip);
                    }
                }
            }
        }
        else
        {
            foreach (var item in data.clips)
            {
                animationDic.Add(item.kay.name, item.value);
            }
        }

        data.clips.Clear();
        if (data.animatorController != null)
        {
            AnimatorOverrideController controller = new AnimatorOverrideController(data.animatorController);
            List<KeyValuePair<AnimationClip, AnimationClip>> list2 = new List<KeyValuePair<AnimationClip, AnimationClip>>();
            controller.GetOverrides(list2);
            foreach (var item in list2)
            {
                AnimationClip value = null;
                if (animationDic.ContainsKey(item.Key.name))
                {
                    value = animationDic[item.Key.name];
                }
                data.clips.Add(new AnimationClipOverride(item.Key, value));
            }
        }
        modelEventTab.UpdateSelect(0);
    }
}
